<template>
    <div>
        <ul>
            <li v-for="(item,index) in shopping" :key="item.id">
                <div>商品名称:{{item.title}} 价格:{{item.price}} 库存:{{item.inventory}} <button @click="buy(item)" :disabled="!item.inventory">购买</button></div>
            </li>
        </ul>
    </div>
</template>
<script setup>
import { useUser } from '../../../store/usrs';
import {storeToRefs} from "pinia"
let userStore =useUser()
// let {all_data}=storeToRefs(userStore)
let {shopping,cars,aa}=storeToRefs(userStore)
userStore.all_data()
console.log(shopping,'ad1')
let buy =(item)=>{
    let addlist={
        name:item.title,
        num:1,
        price:item.price,
        id:item.id
    }
    console.log(addlist,'wwww')
    console.log(cars.value,'1211')
    let target=cars.value.find((i)=>{
        return i.id===addlist.id
    })
    console.log(target,'target')
    if(!target){
        cars.value.push(addlist)
        item.inventory--
    }else{
        target.num++
        item.inventory--
    }
    if(item.inventory<1){
        aa.value=true
        console.log(aa)
    }
}
</script>
<style scoped>
.active{
    pointer-events: none;
}
</style>